package com.cainaer.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cainaer.common.core.constant.UserConstants;
import com.cainaer.common.core.utils.DateUtils;
import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.utils.bean.BeanUtils;
import com.cainaer.shop.domain.ShopInfo;
import com.cainaer.shop.domain.ShopLogin;
import com.cainaer.shop.domain.vo.ShopVo;
import com.cainaer.shop.mapper.ShopLoginMapper;
import com.cainaer.shop.service.IShopInfoService;
import com.cainaer.shop.service.IShopLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商家登录 服务实现类
 * </p>
 *
 * @author Tina
 * @since 2020-09-03
 */
@Service
public class ShopLoginServiceImpl extends ServiceImpl<ShopLoginMapper, ShopLogin> implements IShopLoginService {

    @Autowired
    private ShopLoginMapper shopLoginMapper;

    @Autowired
    private IShopLoginService shopLoginService;

    @Autowired
    private IShopInfoService shopInfoService;

    /**
     * 商家登录
     * 如果不存在就新增
     *
     * @param shopVo
     * @return
     */
    @Override
    public ShopLogin sign(ShopVo shopVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(shopVo.getOpenId())) {
            queryWrapper.eq("open_id", shopVo.getOpenId());
            ShopLogin shopLogin = shopLoginMapper.selectOne(queryWrapper);
            if (!StringUtils.isNull(shopLogin)) {
                ShopInfo info = shopInfoService.queryShopInfoByShopId(shopVo.getShopId());
                shopLogin.setShopInfo(info);
                return shopLogin;
            } else {
                ShopLogin shop = new ShopLogin();
                try {
                    BeanUtils.copyBeanProp(shop, shopVo);

                    shop.setCreateTime(DateUtils.getTime());
                    Integer integer = shopLoginService.insertShop(shop);
                    ShopInfo shopInfo = new ShopInfo();
                    shopInfo.setCreateTime(DateUtils.getTime());
                    BeanUtils.copyBeanProp(shopInfo, shopVo);
                    shopInfo.setShopId(shop.getShopId());
                    Integer insertShopInfo = shopInfoService.insertShopInfo(shopInfo);
                    shop.setShopInfo(shopInfo);
                    return shop;
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
        return null;
    }

    /**
     * 新增商家
     *
     * @param shopLogin
     * @return
     */
    @Override
    public Integer insertShop(ShopLogin shopLogin) {
        shopLogin.setIdentity(0);
        shopLogin.setStatus(0);
        return shopLoginMapper.insert(shopLogin);
    }

    /**
     * 查询商家信息
     *
     * @param shopId
     * @return
     */
    @Override
    public ShopLogin selectShopById(Long shopId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isNull(shopId)) {
            queryWrapper.eq("shop_id", shopId);
            return shopLoginMapper.selectOne(queryWrapper);
        }
        return null;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param tel 手机号码
     * @return
     */
    @Override
    public String checkPhoneUnique(String tel) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("tel", tel);
        ShopLogin login = shopLoginMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(login)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 编辑商家登录表
     *
     * @param shopLogin
     * @return
     */
    @Override
    public Integer editShopLogin(ShopLogin shopLogin) {
        return shopLoginMapper.updateById(shopLogin);
    }
}
